﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AresCustomYacht
{
    public partial class Boats : System.Web.UI.Page
    {
        readonly BL _bl = new BL();
        protected void Page_Load(object sender, EventArgs e)
        {

            var mode = Utility.GetPageMode(this);
            btnAddNew.Visible = true ;
            btnViewCost.Visible = true;
            btnAddCost.Visible = true;
            switch(mode)
            {
                case 0: //Add Cost
                    btnAddNew.Visible = false;
                    btnViewCost.Visible = false;
                    break;
                case 1: //view cost
                    btnAddNew.Visible = false;
                    btnAddCost.Visible = false;
                    break;
                case 2:
                    btnViewCost.Visible = false;
                    btnAddCost.Visible = false;
                    break;
            }
            if (!Page.IsPostBack) // if it is not a post back load things into the list box
            {
                if (User.IsInRole("Customer")) //If the user is customer. No need to load this page - go directly to the customer home which is the report page
                {
                    var linkAddress = Utility.Encrypt("Mode=0&BoatId=" + _bl.GetBoat((Guid)Membership.GetUser().ProviderUserKey).BoatId + "&Message=" + lstBoats.Text);
                    Response.Redirect("Main-Reports.aspx?k=" + linkAddress );
                    return;
                }

                var boats = _bl.GetBoats();
                lstBoats.DataSource = boats;
                lstBoats.DataTextField = "Name";
                lstBoats.DataValueField = "Id";
                lstBoats.DataBind();
                lstBoats.SelectedIndex = 0;
            }


        }

        protected void lstBoats_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        protected void btnViewCost_Click(object sender, EventArgs e)
        {
            //go to reports home with the selected boat
            var linkAddress = Utility.Encrypt("Mode=0&BoatId=" + lstBoats.SelectedValue + "&Message=" + lstBoats.Text );
            Response.Redirect("Main-Reports.aspx?k=" + linkAddress);
        }

        protected void btnAddCost_Click(object sender, EventArgs e)
        {
            //go to the work category page with the selected boat - keep the boat Id all the way untill the report is displayed
            var linkAddress = Utility.Encrypt("Mode=0&BoatId=" + lstBoats.SelectedValue + "&Message=" + lstBoats.SelectedItem.Text );
            Response.Redirect("P-Division.aspx?k=" + linkAddress);
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("Boat.aspx");
        }
    }
}